package com.zhuiyi.yiqingbackend.service.impl;

import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.domain.Base;
import com.zhuiyi.yiqingbackend.domain.request.ChangeBaseStatus;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.mapper.BaseMapper;
import com.zhuiyi.yiqingbackend.service.BaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class BaseServiceImpl implements BaseService {

    @Resource
    private BaseMapper baseMapper;
    @Override
    public List<Base> getBases() {
        return baseMapper.getBases();
    }

    @Override
    public boolean addBase(Base base) {
        //判断是否存在
        String phone = base.getPhone();
        if(baseMapper.getBaseByPhone(phone) != null){
            throw new BusinessException(ErrorCode.DATA_EXIST, "该电话号码已存在");
        }
        return baseMapper.addBase(base);
    }

    @Override
    public boolean deleteBaseById(int id) {
        Base base = baseMapper.getBaseById(id);
        if(base == null){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "删除失败");
        }
        return baseMapper.deleteBaseById(id);
    }

    @Override
    public boolean updateBase(Base base) {
        Base oldBase = baseMapper.getBaseById(base.getId());
        Base old = baseMapper.getBaseByPhone(base.getPhone());
        if(oldBase == null){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "更新失败,没有该用户");
        }
        if(old != null && old.getId() != oldBase.getId()){
            throw new BusinessException(ErrorCode.DATA_EXIST, "更新失败,该电话号码重复");
        }
        return baseMapper.updateBase(base);
    }

    @Override
    public Base getBaseById(int id) {
        Base base = baseMapper.getBaseById(id);
//        if (base == null) {
//            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "该用户不存在");
//        }
        return base;
    }
    public List<Base> getBaseById2(int id) {
        Base base = baseMapper.getBaseById(id);
//        if (base == null) {
//            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "该用户不存在");
//        }
        List<Base> list = new ArrayList();
        list.add(base);
        return list;
    }

    public Base getBaseByPhone(String phone) {
        //判断为空
//        if (phone == null || phone.equals("")) {
//            throw new BusinessException(ErrorCode.PARAM_ERROR);
//        }

        //查询有无数据
        Base base = baseMapper.getBaseByPhone(phone);
        return base;

    }

    @Override
    public boolean updateBaseStatus(ChangeBaseStatus changeBaseStatus) {
        int id = changeBaseStatus.getId();
        int status = changeBaseStatus.getUstatus();
        Base base = baseMapper.getBaseById(id);
        if(base == null){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "更新失败,没有该用户");
        }
        base.setUstatus(status);
        boolean b = baseMapper.updateBase(base);
        if(!b){
            throw new BusinessException(ErrorCode.UPDATE_FAIL, "状态更新失败");
        }
        return b;
    }
}
